package in.muton.edushare.poll.pages;

import in.muton.edushare.poll.sections.PollResultsPanel;
import in.muton.edushare.url.URLLoader;

import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseException;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.UserPollDAO;
import com.edushare.common.vo.Poll;

public class PollResults extends PollBase {

	public PollResults(PageParameters params) {
		super(params);
		Poll p = getPollModel().getObject();
		boolean isOpen = p.getPollResultsPublic() == Poll.POLL_RESULT_OPEN;
		boolean isParticipant = new UserPollDAO(getSessionORMInterface())
				.getPollRespondents(p).contains(getLoggedInUser());
		boolean isReviewer = getLoggedInUser() == p.getCreator()
				|| new UserPollDAO(getSessionORMInterface())
						.getPollObservers(p).contains(getLoggedInUser());

		if (!((isOpen && isParticipant) || isReviewer)) {
			throw new RestartResponseException(URLLoader
					.getPageClass(URLConstants.UNAUTHORIZED_PATH));
		}
		add(new PollResultsPanel("result", getPollModel(), this));
	}

	@Override
	protected String getPageName() {
		return POLL_RESULT_STRING;
	}
	public static final String POLL_RESULT_STRING = "PollResults";
}
